package lib

import "fmt"

func SetMQInit(queue, key string) (*MQ, error) {

	mqconn := NewMQ()
	err := mqconn.Ch.ExchangeDeclare(ExchangeUser, "direct", false, false, false, false, nil)
	if err != nil {
		fmt.Println(err)
		return nil, err
	}

	q := fmt.Sprintf("%s", queue)
	err = mqconn.DecQueueAndBind(q, key, ExchangeUser)
	if err != nil {
		fmt.Println(err)
		return nil, err
	}

	return nil, nil
}

func MQInit() (*MQ, error) {

	mqconn := NewMQ()
	err := mqconn.Ch.ExchangeDeclare(ExchangeUser, "direct", false, false, false, false, nil)
	if err != nil {
		fmt.Println(err)
		return nil, err
	}

	q := fmt.Sprintf("%s", QueueNewUser)
	err = mqconn.DecQueueAndBind(q, RouterKeyUserReg, ExchangeUser)
	if err != nil {
		fmt.Println(err)
		return nil, err
	}

	return mqconn, nil
}

//NewDelayedInitMQ 延迟发送消息
func NewDelayedInitMQ() error {

	mqconn := NewMQ()

	//延迟发送消息
	err := mqconn.Ch.ExchangeDeclare(ExchangeUserDelayed, "x-delayed-message",
		false, false, false, false,
		map[string]interface{}{"x-delayed-type": "direct"})

	if err != nil {
		fmt.Println(err)
		return err
	}

	err = mqconn.DecQueueAndBind(QueueNewUser, RouterKeyUserReg, ExchangeUserDelayed)
	if err != nil {
		fmt.Println(err)
		return err
	}
	return nil
}
